﻿#region Header

/*
Remote views based on linq
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System.IO;
using System.Windows;
using System.Windows.Markup;
using System.Xml;
using Sebarf.Core;
using Sebarf.Core.Interfaces;
using Sebarf.Diagnostics.Interfaces;
using Sebarf.ProcessChains.Interfaces;
using Sebarf.Services.Interfaces;
using Utils;

#endregion

namespace Sebarf.Profiles.WPF {
	/// <summary>
	/// Description of the class
	/// </summary>
	public class WpfProfileLoadRequestHandler : RequestHandler<ProfileLoadRequest> {
		protected override object OnProcessRequest(ProfileLoadRequest request, IServiceLocatorService serviceLocator) {
			string name = request.ProfileId;
			string xamlFileName = request.ProfileLoadArguments["ResourceFileName"].ToString();
			string fileName = Directory.GetCurrentDirectory() + "\\" + xamlFileName;
			Logger.WriteInformation("Loading:" + fileName);
			var dictionary = XamlReader.Load(XmlReader.Create(fileName)) as ResourceDictionary;
			Check.AssertException(dictionary.Contains(name), "profile not exits");
			var profileConfig = dictionary[name] as ProfileConfiguration;

			var serviceRegister = serviceLocator.GetRegisteredService<IServiceRegistrationService>();

			foreach (IServiceImpl service in profileConfig.Services) {
				serviceRegister.RegisterService(service);
			}
			var processRegister = serviceLocator.GetRegisteredService<IProcessChainRegisterService>();
			foreach (IProcessStep step in profileConfig.Processes) {
				processRegister.RegisterProcessChain(step);
			}
			return true;
		}
	}
}